package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.constant.SysConstants;
import com.bsg.upm.entity.PrivilegeEntity;
import com.bsg.upm.entity.ServEntity;
import com.bsg.upm.util.IpV4Utils;

/**
 * 直连用户业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class ServDirectUserCheck extends BaseCheck {

	/**
	 * 直连用户新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 直连用户删除时检查
	 * 
	 * @param serv
	 *            服务对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(ServEntity serv) {
		CheckResult chkRS = new CheckResult();

		// 服务存在性检查
		if (serv == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "服务"));
			return chkRS;
		}

		if (!SysConstants.SERV_UPSQL_UPPROXY_SWM_CODE.equals(serv.getOrder().getDefinitionServ().getCode())) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("该类型服务无直连用户");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		String servId = (String) paramMap.get("servId");
		if (StringUtils.isBlank(servId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "服务编码"));
			return chkRS;
		}

		// 用户名非空和长度检查
		String username = (String) paramMap.get("username");
		if (StringUtils.isBlank(username)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户名"));
			return chkRS;
		}
		if (StringUtils.trim(username).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户名", 32));
			return chkRS;
		}

		// 密码非空和长度检查
		String password = (String) paramMap.get("password");
		if (StringUtils.isBlank(password)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "密码"));
			return chkRS;
		}
		if (StringUtils.trim(password).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "密码", 32));
			return chkRS;
		}

		// 数据库名非空检查
		String databaseName = (String) paramMap.get("databaseName");
		if (StringUtils.isBlank(databaseName)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "数据库名"));
			return chkRS;
		}

		// IP地址非空和有效性检查
		@SuppressWarnings("unchecked")
		List<String> ips = (List<String>) paramMap.get("ips");
		if (ips == null || ips.size() == 0) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "IP地址"));
			return chkRS;
		}
		for (String ip : ips) {
			if (!IpV4Utils.ipV4Validate(ip)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.FORMAT_NOT_ALLOWED, "IP地址"));
				return chkRS;
			}
		}

		// 权限非空检查
		@SuppressWarnings("unchecked")
		List<String> privileges = (List<String>) paramMap.get("privileges");
		if (privileges == null || privileges.size() == 0) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "权限"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		String servId = (String) paramMap.get("servId");
		ServEntity serv = servDao.getBase(servId);
		if (serv == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "服务"));
			return chkRS;
		}

		if (!SysConstants.SERV_UPSQL_UPPROXY_SWM_CODE.equals(serv.getOrder().getDefinitionServ().getCode())) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("该类型服务无直连用户");
			return chkRS;
		}

		List<PrivilegeEntity> dbprivileges = privilegeDao.list(null);
		@SuppressWarnings("unchecked")
		List<String> privileges = (List<String>) paramMap.get("privileges");
		for (String privilegeStr : privileges) {
			PrivilegeEntity result = findPrivilege(dbprivileges, privilegeStr);
			if (result == null) {
				chkRS.setStatus(HttpStatus.SC_GONE);
				chkRS.setErrorMsg(privilegeStr + "权限不存在");
				return chkRS;
			}

			if (!result.getEnabled()) {
				chkRS.setStatus(HttpStatus.SC_GONE);
				chkRS.setErrorMsg(privilegeStr + "权限已被禁用");
				return chkRS;
			}
		}
		return chkRS;
	}

	private PrivilegeEntity findPrivilege(List<PrivilegeEntity> privileges, String privilegeStr) {
		for (PrivilegeEntity privilege : privileges) {
			if (privilege.getCode().equals(privilegeStr)) {
				return privilege;
			}
		}
		return null;
	}
}
